package top.tagao.multithread_02_use.exercise;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/29 20:28
 * 练习：创建两个分线程，其中一个线程遍历100以内的偶数，另一个线程遍历100以内的奇数
 */

public class ex03 {
    public static void main(String[] args) {
        //way01();
        way02();
    }

    /**
     * @date Thead类匿名内部类：其实就是匿名子类对象
     */
    public static void way01() {
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    if (i % 2 == 0) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }
                }
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    if (i % 2 != 0) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }
                }
            }
        }.start();
    }


    /**
     * @date lambda的方式实现
     */
    public static void way02() {
        new Thread(()->{
                for (int i = 0; i < 100; i++) {
                    if (i % 2 != 0) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }
                }
        }).start();

        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                if (i % 2 == 0) {
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                }
            }
        }).start();
    }
}


